﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using Relocation.Com.Tools;
using Relocation.Com;
using System.Diagnostics;
using Relocation.Base;
using Relocation.Data;



namespace Relocation.UI
{
	public partial class Relocatees_HouseList : Relocatees_HouseListHelp
	{
		public Relocatee _Relocatee;

		public Relocatee Relocatee
		{
			get
			{
				return this._Relocatee;
			}
			set
			{
				this._Relocatee = value;
				this.ObjectQuery = this._Relocatee.Relocatees_House.CreateSourceQuery();
			}
		}
		public Relocatees_House SelectRelocatees_House;

		//意向安置户型的个数
		int houseType1;
		int houseType2;
		int houseType3;
		int houseType4;
		int houseType5;
		int houseType6;
		int houseType7;
		int total;		
		public List<int> houseType = new List<int>();

		public Relocatees_HouseList()
		{
			InitializeComponent();
			Initialize();
		}
		public Relocatees_HouseList(Session session)
			: base(session)
		{
			InitializeComponent();
			Initialize();
		}

		public Relocatees_HouseList(Session session, Relocatee relocatee)
			: this(session)
		{	
			this.Relocatee = relocatee;			
			Recompute();
		}

		private void Initialize()
		{
			ControlTag tag = new ControlTag(new RoleInfo(Session.KEY_ROLE_ADMIN, Session.KEY_ROLE_OPERATOR));
			this.ButtonAdd.Tag = tag;
			this.ButtonDel.Tag = tag;
			this.ButtonEdit.Tag = tag;

		}

		/// <summary>
		/// 根据relocatees_id重新获取最新的relocatees对象，防止其他地方修改relocatees的信息
		/// </summary>
		/// <remarks></remarks>
		private void Recompute()
		{
		
			this.houseType1 = Relocatee.housetype1.ToInt();
			this.houseType2 = Relocatee.housetype2.ToInt();
			this.houseType3 = Relocatee.housetype3.ToInt();
			this.houseType4 = Relocatee.housetype4.ToInt();
			this.houseType5 = Relocatee.housetype5.ToInt();
			this.houseType6 = Relocatee.housetype6.ToInt();
			this.houseType7 = Relocatee.housetype7.ToInt();
			this.total = houseType1 + houseType2 + houseType3 + houseType4 + houseType5 + houseType6 + houseType7;
		}

		/// <summary>
		/// 刷新
		/// </summary>
		/// <remarks></remarks>
		public override void Refresh()
		{
			base.Refresh();
			this.Reload();
			this.Recompute();
		}

		/// <summary>
		/// 调用父窗体的refresh
		/// </summary>
		/// <remarks></remarks>
		private void BaseRefresh()
		{
			RelocateesWindow rw = this.ParentForm as RelocateesWindow;
			if (rw != null)
				rw.Refresh();

		}
		/// <summary>
		/// 分配安置房
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		/// <remarks></remarks>
		public void addItem_Click(object sender, EventArgs e)
		{
			try
			{
				House house = this.GetSelectHouse();
				if (house == null)
					return;

				Relocatees_House relocatees_House = new Relocatees_House();
				relocatees_House.House = house;
				//relocateHouse.Relocatee = Relocatee;
				relocatees_House.money1 = 0;
				relocatees_House.money2 = 0;
				relocatees_House.money3 = 0;
				relocatees_House.money4 = 0;
				relocatees_House.money5 = 0;
				relocatees_House.persons = 0;
				relocatees_House.updated = System.DateTime.Now;
				relocatees_House.relocatees_house_id = Relocation.Service.DataModel.GenerateID(relocatees_House);

				this.Session.DataModel.AddToRelocatees_House(relocatees_House);
				Relocatee.Relocatees_House.Add(relocatees_House);
				Relocatee.UpdateStatus();
				house.isUsed = true;
				//relocateHouse.HouseReference.Load();
				//relocateHouse.House.IsUsed = true;
				this.Session.DataModel.Save(house);
				this.Session.DataModel.Save(Relocatee);

				//this.Session.DataModel.RefreshNode(System.Data.Objects.RefreshMode.StoreWins, Relocatee);
				this.Session.GetMainTreeView().RefreshNode(relocatees_House, EntityState.Added);
				this.BaseRefresh();
			} catch (Exception ex)
			{
				Log.Error(ex.Message);
				MyMessagebox.Error(ex.Message);
			}
		}

		/// <summary>
		/// 返回用户选择的安置房
		/// </summary>
		/// <returns></returns>
		private House GetSelectHouse()
		{
			Recompute();
			selectHouseType();
			HousesSelect hs = null;

			if (this.relocatees_HouseDataGridView.Rows.Count < total)
				hs = new HousesSelect(this.Session, houseType);
			else
			{
				if (MyMessagebox.Confirm("已经给该户分配了足够套数的安置房，确定要继续分配吗？") != DialogResult.Yes)
					return null;
				hs = new HousesSelect(Session);
			}
			using (hs)
			{
				hs.ShowDialog();
				return hs.House;
			}
		}



		/// <summary>
		/// 确定目前所需要的安置房的户型
		/// </summary>
		/// <returns></returns>
		/// <remarks></remarks>
		public List<int> selectHouseType()
		{
			foreach (DataGridViewRow r in this.relocatees_HouseDataGridView.Rows)
			{
				switch (r.Cells[4].Value.ToString())
				{
					case "0"://70
						houseType1--;
						break;
					case "1"://90
						houseType2--;
						break;
					case "2"://110
						houseType3--;
						break;
					case "3"://130
						houseType4--;
						break;
				}
			}
			if (houseType1 != 0)
				houseType.Add(0);
			if (houseType2 != 0)
				houseType.Add(1);
			if (houseType3 != 0)
				houseType.Add(2);
			if (houseType4 != 0)
				houseType.Add(3);
			return houseType;
		}

		/// <summary>
		/// 修改
		/// </summary>
		public void EditButton_Click(object sender, EventArgs e)
		{
			try
			{
				
				Relocatees_House relocateHouse = this.GetSelectEntity();
				if (relocateHouse == null)
					return;
				if (!relocateHouse.HouseReference.IsLoaded)
					relocateHouse.HouseReference.Load();
				using (RelocateesHouseWindow rhouse = new RelocateesHouseWindow(this.Session, relocateHouse))
				{
					if (rhouse.ShowDialog().Equals(DialogResult.OK))
						this.BaseRefresh();
				}

			} catch (Exception ex)
			{
				Log.Error(ex.Message);
				Debug.WriteLine(ex);
				MyMessagebox.Error(ex.Message);
			}
		}
		

		/// <summary>
		/// 删除
		/// </summary>		
		public void DeleteButton_Click(object sender, EventArgs e)
		{
			Relocatees_House relocatees_House = this.GetSelectEntity();
			if (relocatees_House == null || MyMessagebox.Confirm("您确定要删除吗？") != DialogResult.Yes)
				return;

			this.Session.DataModel.Connection.Open();
			System.Data.Common.DbTransaction transaction = this.Session.DataModel.Connection.BeginTransaction();
			try
			{
				if (!relocatees_House.HouseReference.IsLoaded)
					relocatees_House.HouseReference.Load();
				relocatees_House.House.isUsed = false;
				relocatees_House.House.project_id = this.Session.Project.project_id;
				this.Session.DataModel.Save(relocatees_House.House);	//更新安置房的状态	
				TreeNode node = this.Session.GetMainTreeView().FindNode(relocatees_House);	
				this.Session.DataModel.Delete(relocatees_House);					//删除安置房的分配关系						
				Relocatee.UpdateStatus();
				this.Session.DataModel.Save(Relocatee);
				transaction.Commit();
				node.Remove();
				this.BaseRefresh();				
			} catch (Exception ex)
			{
				transaction.Rollback();
				Log.Error(ex.Message);
				MyMessagebox.Error(ex.Message);
			}
			this.Session.DataModel.Connection.Close();
			this.Reload();
		}
		
		private void Relocatees_HouseList_Load(object sender, EventArgs e)
		{
			SelectEntity(this.SelectRelocatees_House);
		}

		public override DataGridView GetDataGridView()
		{
			return this.relocatees_HouseDataGridView;
		}

	}
}
